<?php
    define('IN_IA', true);
    Workerman\Protocols\Http::sessionStart();
    
    if(!isset($_GET["access_token"])){
        Workerman\Protocols\Http::header("Location: login.php");
        Workerman\Protocols\Http::end();
    }
    $access_token = $_GET["access_token"];
    $sql = "SELECT a.name,b.id as user_groups,b.authority,b.authority_class FROM hf_mes_user a INNER JOIN hf_mes_groups b ON a.groups=b.id WHERE a.access_token='".$access_token."' LIMIT 1";
    $ret = sql_query($sql);
    if(count($ret) <= 0){
        Workerman\Protocols\Http::header("Location: login.php");
        Workerman\Protocols\Http::end();
    }
    $_SESSION['ACCESS_TOKEN'] = $access_token;
    $_SESSION['IA_USER'] = $ret[0]["name"];
    $_SESSION['user_groups'] = $ret[0]["user_groups"];
    $authority = json_decode($ret[0]['authority']);

    $_SESSION['MES_URL']="http://".$_SERVER["SERVER_NAME"].":".DEBUG_PORT;
    $_SESSION['MES_WEB_URL']= "http://".$_SERVER["SERVER_NAME"].":".INTERFACE_WEB_PORT;
    $_SESSION['LOGGER_URL']="http://".$_SERVER["SERVER_NAME"].":".LOGGER_PORT;
    $MES_WEB_URL = $_SESSION['MES_WEB_URL'];
    require_once $_SESSION["IA_ROOT_WEB"] . '/include/function/common.func.php';
    require_once $_SESSION["IA_ROOT_WEB"] . '/include/function/pdo.func.php';

    // $authority_class= json_decode(current($ret)['authority_class']);
    Workerman\Protocols\Http::header("Content-type:text/html;charset=utf-8");
?>
<!DOCTYPE html>
<html>
<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initialscale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <title>HF-MES燧创</title>

    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="font-awesome/css/font-awesome.css" rel="stylesheet">
    <link href="css/plugins/contextMenu/jquery.contextMenu.css" rel="stylesheet">
    <link href="css/plugins/awesome-bootstrap-checkbox/awesome-bootstrap-checkbox.css" rel="stylesheet">
    <link href="css/plugins/dataTables/datatables.min.css" rel="stylesheet">
    <!--<link href="css/plugins/dataTables/jquery.dataTables.min.css" rel="stylesheet">-->
    <link href="css/plugins/dataTables_editor/dataTables.editor.css" rel="stylesheet">
    <link href="css/plugins/datapicker/bootstrap-datepicker3.css" rel="stylesheet">
    <link href="css/plugins/touchspin/jquery.bootstrap-touchspin.min.css" rel="stylesheet">
    <link href="action/css.php" rel="stylesheet">
    <link href="./css/table.css" rel="stylesheet">
    
    <link href="css/plugins/chosen/chosen.css" rel="stylesheet">
    
    <link href="css/plugins/jsTree/style.min.css" rel="stylesheet">
    <link href="css/plugins/jsTree/proton/style.min.css" rel="stylesheet">
    
    <link href="css/animate.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">
    
    <link href="css/plugins/iCheck/custom.css" rel="stylesheet">
    <link href="css/plugins/awesome-bootstrap-checkbox/awesome-bootstrap-checkbox.css" rel="stylesheet">

    <!-- morris -->
    <link href="css/morris/morris-0.4.3.min.css" rel="stylesheet">
    <!-- morris -->

    <!-- sweetalert2 -->
    <link href="css/plugins/sweetalert2/sweetalert2.min.css" rel="stylesheet">
    
     <!--viewer图片查看-->
    <link href="css/plugins/viewer/viewer.css" rel="stylesheet">
    
    
    <link href="css/fileinput.css" rel="stylesheet">
    <link href="themes/explorer-fas/theme.css" media="all" rel="stylesheet" type="text/css"/>
    


    <style>
    [name="nav_module_active"]{
        color: white!important;
    }
    #jstree::-webkit-scrollbar {
        width: 8px;
        background-color: #f1f1f1;
    } 
    #jstree::-webkit-scrollbar-thumb {
        background-color:#c1c1c1;
    }
    #jstree::-webkit-scrollbar-track {
        -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3);
        background-color: #f1f1f1;
    }
    
    <?php
        foreach ($authority as $key => $value) {
            echo ".".$value."{display:none !important;}";
        }
    ?>

    </style>
</head>

<body>
    <div id="wrapper">
        <nav class="navbar-default navbar-static-side" role="navigation">
            <div class="sidebar-collapse">
                <ul class="nav metismenu" id="side-menu">
                    <li class="nav-header">
                        <div class="dropdown profile-element">
                            <span>
                                <a href="./main.php"><img alt="image" class="img-circle" src="img/profile_small.jpg" /></a>
                            </span>
                            <a data-toggle="dropdown" class="dropdown-toggle" href="#">
                                <span class="clear">
                                    <span class="block m-t-xs">
                                        <strong class="font-bold"><?php echo $_SESSION['IA_USER'];?></strong>
                                    </span>
                                    <span class="text-muted text-xs block"> <?php echo $_SESSION['IA_GROUP'];?>
                                        <b class="caret"></b>
                                    </span>
                                </span>
                            </a>
                        </div>
                        <div class="logo-element">
                            HF+
                        </div>
                    </li>
                </ul>

            </div>
        </nav>

            <div id="page-wrapper" class="gray-bg">+
            <div class="row border-bottom">
            <nav class="navbar navbar-static-top white-bg" role="navigation" style="margin-bottom: 0">
                <div class="navbar-header">
                    <a class="navbar-minimalize minimalize-styl-2 btn btn-primary " href="#"><i class="fa fa-bars"></i></a>
                </div>
                    <ul class="nav navbar-top-links navbar-right">
                        <li>
                            <a href="loginout.php">
                                <i class="fa fa-sign-out"></i>注销
                            </a>
                        </li>
                    </ul>
            </nav>
            </div>

          
            <div class="wrapper wrapper-content" id="mod_body" style="padding:0;">
            
            </div>
            <div class="footer">
                <div class="pull-right" name="mod_main_system_usage_input">
                    Usage  CPU:0%  MEM:0% DISK:0%
                </div>
                <div>
                    <strong>Copyright</strong> Guangzhou Suichuang Information Tech Ltd  &copy; 2016-2017
                </div>
            </div>
            </div>
    </div>

    

    <!-- Mainly scripts -->
    <script src="js/jquery-2.1.1.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/plugins/metisMenu/jquery.metisMenu.js"></script>
    <script src="js/plugins/slimscroll/jquery.slimscroll.min.js"></script>
    <script src="js/plugins/contextMenu/jquery.contextMenu.js"></script>

    <script src="js/common.js"></script>
    <!-- Jquery Validate -->
    <script src="js/plugins/validate/jquery.validate.min.js"></script>  

    <!-- Custom and plugin javascript -->
    <script src="js/inspinia.js"></script>
    <script src="js/plugins/pace/pace.min.js"></script>    

    <!-- <script src="js/demo/morris-demo.js"></script> -->
    <script src="js/morris/raphael-2.1.0.min.js"></script>
    <script src="js/morris/morris.js"></script>

    <!-- Sweet alert -->
    <script src="js/plugins/sweetalert2/sweetalert2.min.js"></script>
    
    <!-- Data Table -->
    <script src="js/plugins/dataTables/datatables.min.js"></script>
    <script src="js/plugins/dataTables/buttons.colVis.min.js"></script>
    <script src="js/plugins/dataTables_editor/dataTables.editor.cracked.min.js"></script>
    
    <!-- Chosen -->
    <script src="js/plugins/chosen/chosen.jquery.js"></script>
    
    <!-- Data picker -->
    <script src="js/plugins/datapicker/bootstrap-datepicker.js"></script>
    <script src="js/plugins/datapicker/locales/bootstrap-datepicker.zh-CN.min.js"></script>

    <!-- cookie -->
    <script src="js/plugins/cookie/jquery.cookie.js"></script>

    <!-- jsonFormatter -->
    <script src="js/plugins/jsonFormatter/jsonFormatter.min.js"></script>  
    <script src="js/dataTables.select.min.js"></script>
    <script src="js/plugins/touchspin/jquery.bootstrap-touchspin.min.js"></script>
 
    <!-- Flot -->
    <script src="js/jquery.flot.js" type="text/javascript"></script>
    <script src="js/jquery.flot.time.js" type="text/javascript"></script>
    <script src="js/jquery.flot.navigate.js" type="text/javascript"></script>
    <script src="js/plugins/flot/jquery.flot.tooltip.min.js"></script>
    <script src="js/plugins/flot/jquery.flot.resize.js"></script>
    <script src="js/plugins/flot/jquery.flot.pie.js"></script>
    
    <!-- textcomplete -->
    <script src="js/plugins/textcomplete/jquery.textcomplete.js"></script>
    <script src="js/plugins/jquery-overlay/jquery.overlay.js"></script>
    
    
    <script src="js/plugins/jsTree/jstree.min.js"></script>
    
    <!-- <script src="js/mes_global.js"></script> -->
      
    <!-- iCheck 全选、反选框-->
    <script src="js/plugins/iCheck/icheck.min.js"></script>

     <!--layui-->
    <script src="js/plugins/layer/layer.js"></script>
    <script type="text/javascript" src="js/plugins/echarts/echarts.min.js"></script>
    <script type="text/javascript" src='./js/table.js'></script>
    
    <!--viewer图片查看-->
    <script src="js/plugins/viewer/viewer.js"></script>
    
    <script src="js/jquery.print.js"></script>

     <!-- 右键插件 -->
    <link rel="stylesheet" href="css/context.standalone.css">
    <script src="js/context.js"></script>
    <!-- 工步插件 -->
    <link rel="stylesheet" href="css/process.css">
    <script src="js/process.js"></script>
    
     <!-- 拖动文件上传 -->
    <script src="js/fileinput.js" type="text/javascript"></script>
    <script src="js/locales/zh.js" type="text/javascript"></script>
    <script src="themes/fas/theme.js" type="text/javascript"></script>
    <script src="themes/explorer-fas/theme.js" type="text/javascript"></script>


    <script src="js/laydate/laydate.js" type="text/javascript"></script>

    <script src="js/fabric.min.js" type="text/javascript"></script>
    
<script>
    
    function load_tag_click(){
        $('#side-menu li:not(.nav-header)').map((k,v)=>{
            if(!$(v).find('li').length){
                let a = $(v).find('a') ; 
                if(a.length){
                    a.click(function() {
                        $('[name="nav_module_active"]').removeAttr('nav_module_active');
                        let file_name = a.attr('class') ,folder_name = folder_name_conf[file_name];
                        if(file_name!=="mod_debuger")a.attr('nav_module_active');
                        log(file_name)
                        $.get("./modules/"+folder_name+"/"+file_name+".php",function(result){
                            $("#mod_body").html(result);
                            $.getScript("./modules/"+folder_name+"/"+file_name+".js.php");  
                        });
                    });
                }
            }
        });
    }

    function clear_tags(li){
        let mi = li.closest('.main_li');
        let si_li = mi.siblings('.main_li');
        si_li.removeClass('active');
        si_li.map((k,v)=>{
            $(v).find('ul.in').removeClass('in');
            $(v).find('li.active').removeClass('active');
        });
    }



    function nav_tag_click(){
            $('#side-menu [name="main_title"],#side-menu [name="sub_title"]').click((e)=>{
                let li = $(e.target).closest('li'),
                    ul =li.children('ul');
                if(!li.hasClass('active')){
                    clear_tags(li);
                    li.addClass('active');
                    ul.addClass('in');
                }else{
                    li.removeClass('active');
                    ul.removeClass('in');
                }
            });
    }




    function init_main(){
        // 获取模块权限
        var param = {url:'<?=$MES_WEB_URL?>'+'/get_conf',type:"POST",dataType:"JSON",data:{action:"get_authority",id:<?= $_SESSION['user_groups'] ?>},access_token:"<?php echo $access_token?>"};
        ajax_request(param,function(result){
            var result = result["rtval"];
            if(result){
                let authority_class= JSON.parse(result['authority_class']);
                // 获取模块配置
                var authority_class_param = {url:'<?=$MES_WEB_URL?>'+'/get_conf',type:"POST",dataType:"JSON",data:{action:"get_modules_conf"},access_token:"<?php echo $access_token?>"};
                ajax_request(authority_class_param,function(result){
                    var result = result["rtval"];
                    folder_name_conf = result.folder_name_conf ;
                    page_conf = result.page_conf ;
                    module_title_conf = result.module_title_conf ;
                    icon_conf = result.icon_conf ;

                    // log(page_conf,module_title_conf,folder_name_conf,authority_class);
                    create_modules(page_conf,module_title_conf,folder_name_conf,authority_class,icon_conf);

                    setTimeout(()=>{
                        nav_tag_click(page_conf,module_title_conf,folder_name_conf);
                        load_tag_click();   //  点击加载子页面
                    }, 1000 );
                })
                
            }  
        })
         
    }

    // 使用配置文件只选择显示元素渲染
    function create_modules(page_conf,module_title_conf,folder_name_conf,authority_class,icon_conf){
            var ele = $('#side-menu') ;
            let html = '';
            let is_debug = false;
            for(main_title in page_conf){
                if(main_title==="mod_debuger"){
                    if(authority_class.indexOf(main_title)===-1){
                        is_debug = true ;
                    }
                    continue;
                }
                let m_html = '';
                let sub_module_arr  = page_conf[main_title] ;
                let type = typeof sub_module_arr[0] ;
                
                console.log(sub_module_arr, type,main_title);
                m_li_s2 = '<li class="main_li"><a href="#" class="'+page_conf[main_title]+'" name="main_title"><i class="'+icon_conf[main_title]+'"></i><span class="nav-label">'+module_title_conf[main_title]+'</span></a>';

                m_li_s = '<li class="main_li"><a href="#" class="modules '+ main_title +'" name="main_title"><i class="'+icon_conf[main_title]+'"></i><span class="nav-label">'+module_title_conf[main_title]+'</span><span class="fa arrow"></span></a>';
                m_li_e = '</li>' ;
                ul_s =  '<ul class="nav nav-second-level collapse">';        
                ul_e =  '</ul>'; 
                
                if(typeof(sub_module_arr)==="object"){
                    for(sub_title in sub_module_arr){
                        let s_html = '' ;
                        if(isNaN(sub_title*1)){      // 关联
                            s_li_s ='<li><a href="#" class="modules '+ sub_title +'" name="sub_title"><span class="nav-label">'+module_title_conf[sub_title]+'</span><span class="fa arrow"></span></a>';
                            s_li_e ='</li>' ;

                            let mo_html = '';    
                            for(module_title of sub_module_arr[sub_title]){
                                if(authority_class.indexOf(module_title)===-1){
                                    li = '<li><a name="module_title" href="#" class="'+ module_title +'">'+ module_title_conf[module_title] +'</a></li>' ;
                                    mo_html += li;
                                }
                            }
                            s_html += mo_html;
                            if(mo_html){    // 回滚1
                                s_html = s_li_s+ul_s+s_html+ul_e+s_li_e ;
                            }  
                        }else{                  // 索引
                            module_title = sub_module_arr[sub_title];
                            if(authority_class.indexOf(module_title)===-1){
                               li = '<li><a name="module_title" href="#" class="'+ module_title +'">'+ module_title_conf[module_title] +'</a></li>' ;
                               s_html += li;
                            }
                        }
                        m_html += s_html;
                    }

                    if(m_html){ // 回滚2
                        m_html = m_li_s + ul_s+ m_html + ul_e + m_li_e ;
                    }
                }else{
                    // log(page_conf[main_title]);
                    m_html= m_li_s2 + m_li_e  ;    //debug
                }

                html += m_html;

            }
            if(is_debug){
                html += '<li class="landing_link debuger modules main_li" style="display: block;"><a href="#" id="btn_debuger" class="mod_debuger"><i class="fa fa-star"></i> <span class="nav-label">Debuger</span> <span class="label label-warning pull-right">NEW</span></a></li>'
            }
            ele.append(html); 
    }

    
    function clearIntervals(){
        for(var i=0; i<100; i++){
            clearInterval(i);
        }
    }


    function get_sys_status(){
            let json = JSON.stringify({"action":"get_sys_status","param":[]});
            $.ajax({
                type: "POST",
                url: "<?php echo $_SESSION['MES_URL'];?>",
                contentType: "application/x-www-form-urlencoded",
                dataType: "json",
                data: json,
                timeout: 1000, 
                success: function (result) {
                    var html = "Usage  CPU:"+result.rtval.cpu+"%  MEM:"+result.rtval.memory+"% DISK:"+result.rtval.disk+"%";
                    $('[name="mod_main_system_usage_input"]').html(html);
                },
                error:function(e){
                }
            });
          
    }

    $(function() {
        // 获取系统状态值
        //get_sys_status();
        // 初始化导航栏
        init_main();
        document.oncontextmenu = function() {return false;};
        $(this).parent().attr("class","active");

        
    });

    
    jQuery.extend(jQuery.validator.messages, {
        required: "必选字段",
        remote: "该值已存在，请换个输入值",
        email: "请输入正确格式的电子邮件",
        url: "请输入合法的网址",
        date: "请输入合法的日期",
        dateISO: "请输入合法的日期 (ISO).",
        number: "请输入合法的数字",
        digits: "只能输入整数",
        creditcard: "请输入合法的信用卡号",
        equalTo: "请再次输入相同的值",
        accept: "请输入拥有合法后缀名的字符串",
        maxlength: jQuery.validator.format("请输入一个长度最多是 {0} 的字符串"),
        minlength: jQuery.validator.format("请输入一个长度最少是 {0} 的字符串"),
        rangelength: jQuery.validator.format("请输入一个长度介于 {0} 和 {1} 之间的字符串"),
        range: jQuery.validator.format("请输入一个介于 {0} 和 {1} 之间的值"),
        max: jQuery.validator.format("请输入一个最大为 {0} 的值"),
        min: jQuery.validator.format("请输入一个最小为 {0} 的值")
    });
    
    var chosen_config = {
        '.chosen-select'  : {allow_single_deselect: true},
        '.chosen-select-deselect'  : {allow_single_deselect:true},
        '.chosen-select-no-single' : {disable_search_threshold:10},
        '.chosen-select-no-results': {no_results_text:'Oops, nothing found!'},
        '.chosen-select-width'     : {width:"95%"}
    }

    $.validator.setDefaults({ ignore: ":hidden:not(.chosen-select)" }) //for all select having class .chosen-select
    $.validator.addMethod('ip4checker', function(value) {
            var ip = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
            return ip.test(value);
    }, 'IP值错误，请重新输入');
    
    $.validator.addMethod("notNumFirstChecker",function(value,element,params){  
            var notNumFirstChecker = /^[a-zA-Z]{1}([a-zA-Z0-9]|[._]){1,19}$/;  
            return this.optional(element)||(notNumFirstChecker.test(value));  
    },"只能含字母数字与符号’_‘, 第一个字符必须为字母"); 
</script>  

</body>
</html>


